﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using DataAccessLayer;

namespace PresentationLayer
{
    public partial class DelegateAuthority : System.Web.UI.Page
    {
        //String userName = "COMM_Head";
        String userName;
        DelegateController delegateController = new DelegateController();
        int ActingManagerID;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Session["UserLoggedIn"] == null)
                {
                    Response.Redirect("login.aspx");
                }
                else
                {
                    List<String> loginValues = (List<String>)(Session["UserLoggedIn"]);
                    if (loginValues[6] == "4" || loginValues[6] == "7") //check whether login user's role is equal to Dept Head or store manager
                    {
                        userName = loginValues[3];
                    }
                    else
                    {
                        Response.Redirect("login.aspx");
                    }

                }
                if (!IsPostBack)
                {

                    ActingManagerID = delegateController.getActingManagerID(userName);

                    if (ActingManagerID != 0)
                    {
                        //if there is data for ActingManagerID in department table
                        //get info of ActingManager
                        ActingManager am = delegateController.getActingManagerInfo(ActingManagerID);

                        //fill up all data
                        ddlEmployee.Items.Add(am.Employee.EmployeeName);
                        hiddenEmpID.Value = am.EmployeeID.ToString();
                        txtStartDate.Text = am.StartDate.ToString();
                        txtReason.Text = am.Reason;
                        btnDelegate.Text = "Revoke";
                        btnCancel.Visible = false;
                        txtReason.Enabled = false;
                        btnStartDate.Enabled = false;
                    }

                    else
                    {
                        //if there is no data for ActingManagerID
                        ddlEmployee.DataSource = delegateController.getEmpList(userName);
                        ddlEmployee.DataTextField = "EmployeeName";
                        ddlEmployee.DataValueField = "EmployeeID";
                        ddlEmployee.DataBind();

                    }
                }
            }
            catch(Exception ex)
            { 
            
            }
            

        }

        protected void btnStartDate_Click(object sender, ImageClickEventArgs e)
        {
            Calendar1.Visible = true;
        }

        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            String sDate = Calendar1.SelectedDate.ToShortDateString();
            DateTime startDate = Convert.ToDateTime(sDate);
            if (startDate < DateTime.Today)
            {
                txtStartDate.Text = "Invalid";
            }
            else
            {
                txtStartDate.Text = sDate;
                Calendar1.Visible = false;
            }

        }

        protected void btnDelegate_Click(object sender, EventArgs e)
        {
            try
            {
                int delegationId;

                if (btnDelegate.Text == "Delegate")
                {
                    //delegate the authority
                    int empId = Convert.ToInt32(ddlEmployee.SelectedValue.ToString());
                    DateTime startDate = Convert.ToDateTime(txtStartDate.Text);
                    String reason = txtReason.Text;

                    delegationId = delegateController.saveToActingManager(empId, startDate, reason);
                    delegateController.sendNotificationtToDelegatedPerson(empId, userName, delegationId, "Delegate");
                    Response.Redirect("~/DelegationSuccessful.aspx?status=Delegate");

                }
                else
                {
                    //revoke the authority
                    int empId = Convert.ToInt32(hiddenEmpID.Value.ToString());
                    delegationId = delegateController.revokeAuthority(empId);
                    delegateController.sendNotificationtToDelegatedPerson(empId, userName, delegationId, "Revoke");
                    Response.Redirect("~/DelegationSuccessful.aspx?status=Revoke");
                }

            }
            catch (Exception ex)
            { 
            
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            txtReason.Text = "";
            txtStartDate.Text = "";
        }
    }
}